package com.simu.towercrane.fragments;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVRelation;
import com.avos.avoscloud.FindCallback;
import com.cn.adapters.ImageAndTextListAdapter.ImageListenr;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.simu.towercrane.R;

public class ImageandDettilsFragment extends BaseFragmentTwo {
	View view = null;
	AVQuery<AVObject> query;
	int currentWhere;
	private Context mContext;
	private ImageListenr mImageListener;
	private ArrayList<String> listImage = new ArrayList<String>();
	private List<Map<String, Object>> mList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContext = getActivity();
		if (view == null) {
			Log.d("fragmentLife", "FragmentTwoonCreateView");
			view = inflater.inflate(R.layout.fragment_product_imageandtext,
					null);
		}
		Tool.destroyParent(view);
		Intent intent = getActivity().getIntent();
		String objectID = intent.getStringExtra("objectID");
		addInfo(objectID);
		return view;
	}

	public void addInfo(String objectID) {
		
		query = new AVQuery<AVObject>("Commodity");
		query.whereEqualTo("objectId", objectID);
		query.findInBackground(new FindCallback<AVObject>() {

			@Override
			public void done(List<AVObject> list, AVException arg1) {
				// TODO Auto-generated method stub
				mList = new ArrayList<Map<String, Object>>();
				mList.clear();
				if (list != null) {

					for (int i = 0; i < list.size(); i++) {
						currentWhere = list.size();
						final HashMap<String, Object> map = new HashMap();
						map.clear();

						AVObject parent = list.get(i);
						parent.getString("name");

						AVRelation<AVObject> avRelation = parent
								.getRelation("imageText");
						AVQuery<AVObject> avQuery = avRelation.getQuery();
						avQuery.findInBackground(new FindCallback<AVObject>() {

							@Override
							public void done(List<AVObject> arg0,
									AVException arg1) {

								if(arg0!=null)
								{
								ViewGroup group = (ViewGroup) view
										.findViewById(R.id.viewGrou_ImageAndText);
								group.removeAllViews();
								for (int k = 0; k < arg0.size(); k++) {
									AVObject avobject = arg0.get(k);
									AVFile avFile = avobject.getAVFile("image");

									String url = avFile.getUrl();
									listImage.add(url);

									ImageView[] imageViews = new ImageView[listImage
											.size()];
									for (int i = 0; i < imageViews.length; i++) {
										final ImageView imageView = new ImageView(
												mContext);
										imageView
												.setLayoutParams(new LayoutParams(
														LayoutParams.MATCH_PARENT,
														LayoutParams.MATCH_PARENT));
										imageView.setPadding(0, 20, 0, 0);
										imageView.setScaleType(ScaleType.CENTER);

										imageViews[i] = imageView;
										ImageLoader.getInstance().displayImage(
												listImage.get(i) + "",
												imageViews[i]);
										// imageViews[i].setOnClickListener(new
										// OnClickListener() {
										//
										// @Override
										// public void onClick(View arg0) {
										// // TODO Auto-generated method stub
										// if(mImageListener!=null){
										// mImageListener.responeImageListner(imageView);
										// }
										// }
										// });
										String Text = arg0.get(k).getString(
												"text");
										TextView textview = new TextView(
												mContext);
										textview.setLayoutParams(new LayoutParams(
												LayoutParams.MATCH_PARENT,
												LayoutParams.MATCH_PARENT));
										textview.setPadding(20, 20, 20, 0);
										textview.setText("\t\t"+Text);
										textview.setTextColor(getResources().getColor(R.color.normal_text_color));
										group.addView(imageView);
										group.addView(textview);
										listImage.clear();
										

									}

								}

							}
							}
						});

					}

				}

			}
		});
	}

	public void setImageListener(ImageListenr iamgeListner) {
		this.mImageListener = iamgeListner;
	}

	public interface ImageListenr {
		public void responeImageListner(ImageView imageView);
	}
}
